Skip to content

Stop native memory leak of ImagickDraw::setFont() in text rendering - #1562

Open
javiermarinros wants to merge 1 commit into
Intervention:developfrom
javiermarinros:fix/imagick-font-leak
Open

javiermarinros wants to merge 1 commit into
Intervention:developfrom
javiermarinros:fix/imagick-font-leak

Conversation

@javiermarinros

Copy link
Copy Markdown
Contributor

ImagickDraw::setFont() leaks native memory on every call in imagick 3.8.1 and earlier: the extension
checks the font against MagickQueryFonts() and frees the list but not the font names in it
(fix pending in Imagick/imagick#786, open since May 2026). The Imagick FontProcessor creates a new
ImagickDraw and calls setFont() for every measure and every line, several times per text() call,
and the driver returned a new FontProcessor each time.

This PR:

  • keeps one FontProcessor per Imagick driver;
  • sets the font once per font file on a prototype ImagickDraw, and clones it in toImagickDraw().
    A clone copies the font without calling setFont() again. Font size and color are still set on the clone.

Native memory (RSS) growth over 2000 images with 3 texts each, after warm-up:

develop this PR
ImageMagick 7.1.1-43, imagick 3.8.1 (105 system fonts) +176 MB (90 KB/image) +0 MB
ImageMagick 6.9.11-60, imagick 3.8.1 (102 system fonts) +198 MB (101 KB/image) +0 MB
ImageMagick 6.9.12-98, imagick 3.7.0 (8 system fonts) +13 MB (6.7 KB/image) +0 MB

The loss grows with the number of fonts installed on the system. Time per image also drops about 10 %
(4.56 → 4.11 ms on IM 7). Rendered output is byte-identical to develop (PNG and JPEG, 18 cases: 3 sizes,
3 alignments, 2 angles, wrap, line height, stroke, 2 texts per image) on all three versions.

The cache is keyed by the real path of the font file, so a relative path keeps working after chdir().

Notes for review:

  • The font file is checked once, when it is first used. If the file is deleted later, the error comes
    from the drawing call instead of toImagickDraw().
  • The driver keeps one ImagickDraw per font file for its lifetime.
  • A driver that has drawn text holds ImagickDraw objects, which PHP cannot serialize. The library
    never serializes drivers.

Tests: FontProcessor returns independent draw objects with their own size and color; the driver
returns the same font processor. Full suite: same result as develop on IM 7.1.1 and 6.9.11 (the
remaining failures come from missing exif/heic support in my environment). phpstan and phpcs pass.

🤖 Generated with Claude Code

@javiermarinros

Copy link
Copy Markdown
Contributor Author

Reproduction, runs in a clean php:8.3-cli container (about 40 s): https://gist.github.com/javiermarinros/b1d2b55b14e47ebe412e2b940c3337e4

git clone https://gist.github.com/b1d2b55b14e47ebe412e2b940c3337e4.git repro && cd repro
docker run --rm -v "$PWD":/repro php:8.3-cli sh /repro/run.sh

It installs imagick, clones develop and this branch, draws 1000 images with 3 texts each, and prints the process memory growth:

develop                RSS + 88.5 MB after 1000 images  (ImageMagick 7.1.1-43, imagick 3.8.1, 105 fonts known to ImageMagick)
fix/imagick-font-leak  RSS +  0.0 MB after 1000 images  (ImageMagick 7.1.1-43, imagick 3.8.1, 105 fonts known to ImageMagick)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant